<template>
  <div>
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/welcome' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片区域 -->
    <el-card>
      <!-- 搜索框和添加商品区域 -->
      <el-row :gutter="20">
        <!-- 搜索框 -->
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="searchInput">
            <el-button
              slot="append"
              icon="el-icon-search"
              @click="SearchGoods"
            ></el-button>
          </el-input>
        </el-col>
        <el-col :span="1.2">
          <el-button type="primary" @click="AddGoodsBtn">添加商品</el-button>
        </el-col>
        <el-col :span="1">
          <el-button type="success" @click="exportData">导出表格</el-button>
        </el-col>
      </el-row>

      <!-- 表格区域 -->
      <el-table :data="goodList" style="width: 100%" border stripe>
        <el-table-column label="#" type="index"> </el-table-column>
        <el-table-column label="商品名称" prop="goods_name"> </el-table-column>
        <el-table-column label="商品价格(元)" prop="goods_price" width="120px">
        </el-table-column>
        <el-table-column label="商品重量" prop="goods_weight" width="80px">
        </el-table-column>
        <el-table-column label="创建时间" prop="add_time" width="150px">
        </el-table-column>
        <el-table-column label="操作" width="120px">
          <template slot-scope="scope">
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="EditGoodsBtn(scope.row.goods_id)"
            ></el-button>
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="DeleteGoods(scope.row.goods_id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页区域 -->
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="queryInfo.pagenum"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="queryInfo.pagesize"
        layout="total, sizes, prev, pager, next, jumper"
        :total="this.total"
      >
      </el-pagination>

      <el-dialog
        title="编辑商品"
        :visible.sync="this.EditGoodsDialogVisible"
        :before-close="EditGoodsDialogClose"
        width="50%"
      >
        <el-form :model="EditGoodsList">
          <el-form-item label="商品名称" prop="goods_name" required>
            <el-input v-model="EditGoodsList.goods_name"></el-input>
          </el-form-item>
          <el-form-item label="商品价格" prop="goods_price">
            <el-input v-model="EditGoodsList.goods_price"></el-input>
          </el-form-item>
          <el-form-item label="数量" prop="goods_number">
            <el-input v-model="EditGoodsList.goods_number"></el-input>
          </el-form-item>
          <el-form-item label="重量" prop="goods_weight">
            <el-input v-model="EditGoodsList.goods_weight"></el-input>
          </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
          <el-button @click="EditGoodsDialogVisible = false">取 消</el-button>
          <el-button type="primary" @click="EditGoods(EditGoodsList.goods_id)"
            >确 定</el-button
          >
        </span>
      </el-dialog>
    </el-card>
  </div>
</template>

<script>
import { timestampToTime } from '../../common/js/timestampToTime'
import { export2Excel } from '../../common/js/util'
export default {
  name: "goodsList",
  data() {
    return {
      // 发起请求需要携带的参数
      queryInfo: {
        query: "",
        pagenum: 1,
        pagesize: 10,
      },
      // 搜索框输入的内容
      searchInput: "",
      // 获取到的商品列表
      goodList: [],
      // 商品总数
      total: 0,
      EditGoodsDialogVisible: false,
      EditGoodsList: {},
      columns: [
        { title: "商品名称", key: "goods_name" },
        { title: "商品价格(元)", key: "goods_price" },
        { title: "商品重量", key: "goods_weight" },
        { title: "创建时间", key: "add_time" },
      ]
    };
  },
  created() {
    // 刚刷新页面就去发动网络请求
    this.getGoodsList();
  },
  methods: {
    // 获取商品数据
    async getGoodsList() {
      const { data: res } = await this.$http.get("goods", {
        params: this.queryInfo,
      });
      if (res.meta.status !== 200) {
        this.$message.error("获取商品数据失败");
      }
      res.data.goods.forEach(item => {
        item.add_time = timestampToTime(item.add_time)
      })
      this.goodList = res.data.goods;
      this.total = res.data.total;
    },
    // 点击当前页后变更当前页，并获取当前页的数据 val为该方法的回调参数，即当前页
    handleCurrentChange(val) {
      this.queryInfo.pagenum = val;
      this.getGoodsList();
    },
    // 点击页码大小，并获取当前页码的数据 val为选择的当前页码大小
    handleSizeChange(val) {
      this.queryInfo.pagesize = val;
      this.getGoodsList();
    },
    // 搜索框功能
    SearchGoods() {
      this.queryInfo.query = this.searchInput;
      this.getGoodsList();
    },
    // 删除功能
    DeleteGoods(id) {
      this.$confirm("此操作将永久删除该商品, 是否继续?", "提示", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const { data: res } = await this.$http.delete(`goods/${id}`);
          if (res.meta.status !== 200) {
            this.$message.error("删除商品失败");
          }
          this.$message({
            message: "删除商品成功",
            type: "success",
            duration: 1500,
          });
          this.getGoodsList();
        })
        .catch((err) => err);
    },
    // 点击添加商品触发
    AddGoodsBtn() {
      this.$router.push("/goods/addgoods");
    },
    // 点击编辑商品时触发
    async EditGoodsBtn(id) {
      const { data: res } = await this.$http.get(`goods/${id}`);
      if (res.meta.status !== 200) this.$message.error("获取商品信息失败!");
      this.EditGoodsList = res.data;
      this.EditGoodsDialogVisible = true;
    },
    // 关闭编辑商品对话框时触发
    EditGoodsDialogClose() {
      this.EditGoodsDialogVisible = false;
    },
    // 编辑商品对话框确定按钮触发
    async EditGoods(id) {
      const { data: res } = await this.$http.put(
        `goods/${id}`,
        this.EditGoodsList
      );
      if (res.meta.status !== 200) this.$message.error("修改商品信息失败!");
      this.$message.success("修改商品信息成功!");
      this.getGoodsList();
      this.EditGoodsDialogVisible = false;
    },
    // 导出表格
    exportData() {
      export2Excel(this.columns, this.goodList, '商品列表')
    },
  },
};
</script>

<style lang="less" scoped>
.el-table {
  margin: 20px 0;
  font-size: 12px;
}

.el-breadcrumb{
    font-size: 14px;
}
</style>
